<template>
	<view class="page">
		<view class="title">
			<input type="text" v-model="title" />
		</view>
		<view class="content">
			<textarea v-model="content" 
			placeholder="请输入内容" />
		</view>
		<view class="control">
			<button type="default"
			@click="back"
			>返回</button>
			<button
			 v-if="title&&content"
			 type="primary"
			 @click="add()"
			 >保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'add',
		data() {
			return {
				title: '',
				content: '',
			}
		},
		methods: {
			add() {
				console.log('add')
				const db = wx.cloud.database();
				db.collection('todos').add({
					data: {
						title: this.title,
						content: this.content,
						lock: false,
						date: new Date().getTime()
					},
					success: res => {
						wx.showToast({
							title: '添加成功！'
						})
					}
				})
			},
			back() {
				uni.redirectTo({
					url: '../list/index'
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.title {
		padding: 30rpx;
		input {
			padding: 20rpx 0;
			border-bottom: 1rpx #ebebeb solid;
		}
	}
	.content {
		padding: 30rpx;
		textarea {
			width: 100%;
			padding: 20rpx;
			height: calc(100vh - 400rpx);
		}
	}
	.control {
		padding: 20rpx;
		display: flex;
		button {
			flex: 1;
			margin: 0 10rpx;
		}
	}
</style>
